/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.categorias;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import modelo.Categoria;
import persistencia.CategoriaBean;

/**
 *
 * @author Felipe
 */
@WebServlet(name = "ListarCategoria", urlPatterns = {"/ListarCategoria"})
public class ListarCategoria extends HttpServlet {

    @Resource(name = "jdbc/HOLAJDBC")
    private DataSource ds;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Connection conexion = null;

        try {
            conexion = ds.getConnection();
            ServletContext contexto = request.getServletContext();
            CategoriaBean bean = new CategoriaBean(conexion);
            Collection<Categoria> categorias = bean.buscarTodos();
            if (null == categorias) {
                request.setAttribute("Mensaje", "NO existen Categorias");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }

            contexto.setAttribute("Categorias", categorias);

            request.getRequestDispatcher("/Informes/Listados/ListarCategorias.jsp").forward(request, response);

        } catch (SQLException ex) {
            Logger.getLogger(ListarCategoria.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conexion.close();
            } catch (SQLException ex) {
                Logger.getLogger(ListarCategoria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection conexion = null;
        try {
            conexion = ds.getConnection();
            String listar = request.getParameter("LISTAR");
            if (listar == null) {
                int codigo = 0;
                
                Categoria cate = new Categoria();
                CategoriaBean bean = new CategoriaBean(conexion);
                if (null != request.getParameter("COD_CATEGORIA")) {
                    try {
                        codigo = Integer.parseInt(request.getParameter("COD_CATEGORIA"));
                    } catch (NumberFormatException ex) {
                        request.setAttribute("Mensaje", "ingrese un codigo valido");
                        request.getRequestDispatcher("/Categorias/formConsultarCategoria.jsp").forward(request, response);
                    }
                    System.out.println("codigo cat a modificar : " + codigo);
                    cate = bean.buscarPorCodigo(codigo);

                } else if (null != request.getParameter("NOMBRE")) {
                    String nombre = request.getParameter("NOMBRE");
                    System.out.println("nombre cat a modificar : " + nombre);
                    cate = bean.buscarPorNombre(nombre);
                }
                if (null == cate) {
                    request.setAttribute("Mensaje", "NO existe tal categoria");
                    request.getRequestDispatcher("Productos/formConsultarCategoria.jsp").forward(request, response);
                }
                request.setAttribute("registro", cate);
                request.getRequestDispatcher("/Admin/Informes/Listados/MostrarCategoria.jsp").forward(request, response);
            }else{
                processRequest(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ListarCategoria.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                conexion.close();
            } catch (SQLException ex) {
                Logger.getLogger(ListarCategoria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
